package com.youlu.campus.website.controller;

import com.youlu.campus.common.base.QResult;
import com.youlu.campus.website.entity.Carousel;
import com.youlu.campus.website.entity.CarouselPageQuery;
import com.youlu.campus.website.service.CarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 轮播图控制器
 */
@RestController
@RequestMapping("/website/carousels")
public class CarouselController {

    @Autowired
    private CarouselService carouselService;

    /**
     * 创建轮播图
     *
     * @param carousel
     * @return
     */
    @PostMapping("/save")
    public QResult<Carousel> createCarousel(@RequestBody Carousel carousel) {
        return QResult.success(carouselService.save(carousel));
    }

    /**
     * 删除轮播图
     *
     * @param carousel
     * @return
     */
    @PostMapping("/delete")
    public QResult<Void> deleteCarousel(@RequestBody Carousel carousel) {
        carouselService.deleteCarousel(carousel.getId());
        return QResult.success();
    }

    /**
     * 根据id查询轮播图
     *
     * @param id
     * @return
     */
    @GetMapping("/info")
    public QResult<Carousel> getCarouselById(@RequestParam String id) {
        return QResult.success(carouselService.getCarouselById(id));
    }

    /**
     * 分页查询所有轮播图
     *
     * @param pageQuery
     * @return
     */
    @PostMapping("/find-page")
    public QResult<Page<Carousel>> getAllCarousels(@RequestBody CarouselPageQuery  pageQuery) {
        return QResult.success(carouselService.findByPage(pageQuery));
    }
}
